package com.ycw.servlet.guoguo;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.ycw.dao.HouseRepairDao;
import com.ycw.dao.impl.HouseRepairDaoImpl;

/**
 * Servlet implementation class ShowHouseRepairDataServlet
 */
@WebServlet("/showHouseRepairDataServlet")
public class ShowHouseRepairServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * 根据输入的页数查询数据（默认显示第一页）
	 */
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 得到页数
		String page = request.getParameter("pageNum");
		if (page == null || "".equals(page)) {
			page = "1";
		}
		Integer pageNum = Integer.parseInt(page);

		HouseRepairDao hrd = HouseRepairDaoImpl.getInstance();
		// 根据页数查询
		Map<String, Object> map = hrd.queryAllHouseRepairData(pageNum);
		// 得到所有数据的数量
		int Count = hrd.queryAllHouseRepairData();
		// 得到总页数
		int pageCount = Count % 10 == 0 ? Count / 10 : Count / 10 + 1;

		map.put("Count", Count);
		map.put("pageCount", pageCount);

		String resJSON = JSON.toJSONString(map);
		PrintWriter out = response.getWriter();
		out.write(resJSON);

	}

}
